package astra.client.pages.purchase;

import astra.client.models.Purchase;
import astra.client.models.TableRequestResult;
import astra.client.pages.Page;
import astra.client.services.DataSource;
import astra.client.utils.widgets.table.AdvancedTable;
import astra.client.utils.widgets.table.TableColumn;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kotelnikov Dmitry <a href="mailto:dimonnot@mail.ru"/>
 */
public class PurchasePage extends Page {
    public final static DateTimeFormat DATE_FORMAT = DateTimeFormat.getFormat("dd.MM");

    interface PurchasePageUiBinder extends UiBinder<HTMLPanel, PurchasePage> {
    }

    private static PurchasePageUiBinder ourUiBinder = GWT.create(PurchasePageUiBinder.class);

    @UiField(provided = true)
    AdvancedTable table;
    @UiField
    FlowPanel actions;
    @UiField
    Button showAddPurchaseDialog;
    @UiField
    Button showAddClientDialog;
    @UiField
    Button showAddSupplierDialog;

    protected PurchasePopup editPopup;

    @UiConstructor
    public PurchasePage(String name) {
        super(name);

        editPopup = new PurchasePopup(astra, service, this);

        List<TableColumn<Purchase>> columns = new ArrayList<TableColumn<Purchase>>();

        columns.add(
            new TableColumn<Purchase>(
                "Дата подтверждения",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return DATE_FORMAT.format(object.getConfirmDate());
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Вид услуги",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getService();
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Адрес",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getAddress();
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Подрядчик",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getSupplier();
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Менеджер",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getManager();
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Аккаунт",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getAccount();
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Материал",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getMaterial();
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Текст рекламы",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getAdvertisment();
                    }
                }
            )
        );
        /*
        columns.add(
            new TableColumn<Purchase>(
                "Юр. лицо",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object;
                    }
                }
            )
        );
        */
        columns.add(
            new TableColumn<Purchase>(
                "Макет",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return (object.isModel() ? "сдан" : "не сдан");
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Фотоотчет",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getPhotoReport();
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Зарплата менеджера",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return Double.toString(object.getManagerSalary());
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Зарплата аккаунта",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return Double.toString(object.getAccountSalary());
                    }
                }
            )
        );

        columns.add(
            new TableColumn<Purchase>(
                "Договор",
                new TextColumn<Purchase>() {
                    @Override
                    public String getValue(Purchase object) {
                        return object.getContractNumber();
                    }
                }
            )
        );

        table = new AdvancedTable<Purchase>(
            columns, new DataSource<Purchase>() {
                @Override
                public void get(
                    int page, int rowsPerPage, boolean requestRowsCount,
                    AsyncCallback<TableRequestResult<Purchase>> async
                ) {
                    service.getPurchase(page, rowsPerPage, requestRowsCount, null, async);
                }
            }
        );

        initWidget(ourUiBinder.createAndBindUi(this));

    }

    @Override
    public void show() {
        showActions(actions);
    }

    @UiHandler("showAddPurchaseDialog")
    public void onClickShowAddPurchaseDialog(ClickEvent event) {
        editPopup.edit(null);
    }

    @UiHandler("showAddClientDialog")
    public void onClickShowAddClientDialog(ClickEvent event) {
        astra.getClientPopup().edit(null);
    }

    @UiHandler("showAddSupplierDialog")
    public void onClickShowAddSupplierDialog(ClickEvent event) {
        astra.getSupplierPopup().edit(null);
    }
}